import { defineStore } from 'pinia'
import defaultSettings from '@/layouts/settings.js'

const PREFIX = import.meta.env.VITE_COOKIE_PREFIX
export const settingKey = PREFIX + '-layout-setting'

const settingDefaultData = {
    title: '',
    theme: '#409EFF',
    sideTheme: undefined,
    showSettings: undefined,
    topNav: undefined,
    tagsView: undefined,
    fixedHeader: undefined,
    sidebarLogo: undefined,
    dynamicTitle: undefined
}

const storageSetting = {...settingDefaultData, ...defaultSettings, ...(JSON.parse(localStorage.getItem(settingKey)) || {})}
localStorage.setItem(settingKey, JSON.stringify(storageSetting))

export default defineStore('useSettingsStore', {
    state: () => reactive({
        ...storageSetting,
    }),
    actions: {
        // 修改布局设置
        changeSetting({key, value}) {
            if (Object.hasOwn(this, key)) {
                this[key] = value
            }
        },
        // 设置网页标题
        setTitle(title) {
            this.title = title
        }
    }
})
